﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using OTS.BLL;

public partial class new_product : System.Web.UI.Page
{
    ProductTypeBLL proType = new ProductTypeBLL();
    ProductBLL productBLL = new ProductBLL();
    BrandBLL brand = new BrandBLL();
    DataTable dt = new DataTable();
    DataTable dTable = new DataTable();
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dt = proType.GetAllType();
            this.rpType.DataSource = dt;
            this.rpType.DataBind();

            dt = brand.GetAllBrand();
            this.rpBrand.DataSource = dt;
            this.rpBrand.DataBind();

            MyDs();
            ViewState["pagen"] = 0; //当前第pagen+1页
        }
    }

    protected void ddlPaixu_SelectedIndexChanged(object sender, EventArgs e)
    {
        PaixuPage(this.ddlPaixu.SelectedValue);
    }

    private void PaixuPage(string paixu) {
        DataTable dt = new DataTable();
        DataTable dTable = new DataTable();
        string type = this.txtType.Text;
        string brand = this.txtBrand.Text;

        if (type != "0" && brand == "0")
        {
            switch (paixu)
            {
                case "0":
                    dt = productBLL.GetAllProduct("where typeID=" + Int32.Parse(type));
                    break;
                case "1":
                    dt = productBLL.GetAllProduct("where typeID=" + Int32.Parse(type) + " order by wholeSalePrice desc");
                    break;
                default:
                    dt = productBLL.GetAllProduct("where typeID=" + Int32.Parse(type) + " order by wholeSalePrice asc");
                    break;
            }
            dTable = productBLL.GetTotal("where typeID=" + Int32.Parse(type));
        }
        else if (brand != "0" && type == "0")
        {
            switch (paixu)
            {
                case "0":
                    dt = productBLL.GetAllProduct("where brandID=" + Int32.Parse(brand));
                    break;
                case "1":
                    dt = productBLL.GetAllProduct("where brandID=" + Int32.Parse(brand) + " order by wholeSalePrice desc");
                    break;
                default:
                    dt = productBLL.GetAllProduct("where brandID=" + Int32.Parse(brand) + " order by wholeSalePrice asc");
                    break;
            }
            dTable = productBLL.GetTotal("where brandID=" + Int32.Parse(brand));
        }
        else if (type != "0" && brand != "0")
        {
            switch (paixu)
            {
                case "0":
                    dt = productBLL.GetAllProduct("where brandID=" + Int32.Parse(brand) + " and typeID=" + Int32.Parse(type));
                    break;
                case "1":
                    dt = productBLL.GetAllProduct("where brandID=" + Int32.Parse(brand) + " and typeID=" + Int32.Parse(type) + " order by wholeSalePrice desc");
                    break;
                default:
                    dt = productBLL.GetAllProduct("where brandID=" + Int32.Parse(brand) + " and typeID=" + Int32.Parse(type) + " order by wholeSalePrice asc");
                    break;
            }
            dTable = productBLL.GetTotal("where brandID=" + Int32.Parse(brand) + " and typeID=" + Int32.Parse(type));
        }
        else
        {
            switch (paixu)
            {
                case "0":
                case "1":
                    dt = productBLL.GetAllProduct("order by wholeSalePrice desc");
                    break;
                default:
                    dt = productBLL.GetAllProduct("order by wholeSalePrice  asc");
                    break;
            }
            dTable = productBLL.GetTotal("");
        }
        proPage(dt, dTable);
    }

    private void MyDs()
    {
        DataTable dt = new DataTable();
        DataTable dTable = new DataTable();
        string type = this.txtType.Text;
        string brand = this.txtBrand.Text;

        if (type != "0" && brand == "0")
        {
            dt = productBLL.GetAllProduct("where typeID=" + Int32.Parse(type));
            dTable = productBLL.GetTotal("where typeID=" + Int32.Parse(type));
        }
        else if (brand != "0" && type == "0")
        {
            dt = productBLL.GetAllProduct("where brandID=" + Int32.Parse(brand));
            dTable = productBLL.GetTotal("where brandID=" + Int32.Parse(brand));
        }
        else if (type!="0" && brand!="0")
        {
            dt=productBLL.GetAllProduct("where brandID=" + Int32.Parse(brand) + " and typeID=" + Int32.Parse(type));
            dTable = productBLL.GetTotal("where brandID=" + Int32.Parse(brand) + " and typeID=" + Int32.Parse(type));
        }
        else
        {
            dt = productBLL.GetAllProduct("order by proID desc");
            dTable = productBLL.GetTotal("");
        } 

        proPage(dt, dTable);
    }

    //分页
    private void proPage(DataTable dt,DataTable dTable) {
        //截取数据库取出的图片信息
        //创建表
        DataSet ds = new DataSet("typeBypro");   //创建一个名为ShowPic的DataSet
        DataTable table = new DataTable("typeBypro");    //创建名为ShowPic的DataTable
        table.Columns.Add(new DataColumn("tb_id", typeof(int)));
        table.Columns.Add(new DataColumn("tb_proNO", typeof(string)));
        table.Columns.Add(new DataColumn("tb_wholeSalePrice", typeof(float)));
        table.Columns.Add(new DataColumn("tb_retailPrice", typeof(float)));
        table.Columns.Add(new DataColumn("tb_zhekou", typeof(string)));
        table.Columns.Add(new DataColumn("tb_showpic", typeof(string)));
        table.Columns.Add(new DataColumn("tb_title", typeof(string)));

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            DataRow dr = table.NewRow();
            dr["tb_id"] = dt.Rows[i]["proID"];
            dr["tb_proNO"] = dt.Rows[i]["proNO"].ToString();
            dr["tb_wholeSalePrice"] = dt.Rows[i]["wholeSalePrice"].ToString();

            //计算折扣
            if (Convert.ToInt32(dt.Rows[i]["retailPrice"].ToString()) == 0 || Convert.ToInt32(dt.Rows[i]["retailPrice"].ToString()) == Convert.ToInt32(dt.Rows[i]["wholeSalePrice"].ToString()))
            {
                dr["tb_retailPrice"] = dt.Rows[i]["wholeSalePrice"].ToString();
                dr["tb_zhekou"] = "0";
            }
            else
            {
                double discount = (Convert.ToInt32(dt.Rows[i]["retailPrice"].ToString()) / Convert.ToInt32(dt.Rows[i]["wholeSalePrice"].ToString())) * 10;
                dr["tb_retailPrice"] = dt.Rows[i]["wholeSalePrice"].ToString();
                dr["tb_zhekou"] = discount.ToString();
            }
            dr["tb_showpic"] = dt.Rows[i]["bigImg_pic"].ToString();
            dr["tb_title"] = dt.Rows[i]["title"].ToString();
            table.Rows.Add(dr);
        }
        ds.Tables.Add(table);
        try
        {
            //分页
            PagedDataSource pgds = new PagedDataSource();
            pgds.DataSource = ds.Tables[0].DefaultView; //数据绑定
            pgds.AllowPaging = true;    //允许分页
            pgds.PageSize = 60;  //每页数据量
            pgds.CurrentPageIndex = (Convert.ToInt32(ViewState["pagen"]));  //设置显示当前第几页
            SetEnable(pgds);    //是否可点

            this.rpTypeByProductList.DataSource = pgds;
            this.rpTypeByProductList.DataBind();
            this.lblTotalPruduct.Text = dTable.Rows[0]["total"].ToString();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    /// <summary>
    /// 设置是否可点
    /// </summary>
    /// <param name="pgds"></param>
    private void SetEnable(PagedDataSource pgds)
    {
        this.lbtnPre.Enabled = true;
        this.lbtnNext.Enabled = true;
        this.lbtnPre2.Enabled = true;
        this.lbtnNext2.Enabled = true;
        if (pgds.CurrentPageIndex == 0)
        {
            this.lbtnPre.Enabled = false;
            this.lbtnPre2.Enabled = false;
            this.lbtnPre.CssClass = "unprev";
            this.lbtnPre2.CssClass = "unprev";
        }
        else if (pgds.CurrentPageIndex == pgds.PageCount - 1)
        {
            this.lbtnNext.Enabled = false;
            this.lbtnNext2.Enabled = false;
            this.lbtnNext.CssClass = "unnext";
            this.lbtnNext2.CssClass = "unnext";
        }
        if (pgds.PageCount == 0)
        {
            this.lblCount.Text = "对不起，没有您要查询的记录！";
            this.lblCount.Visible = true;
            this.lbtnPre.Enabled = false;
            this.lbtnPre2.Enabled = false;
            this.lbtnNext.Enabled = false;
            this.lbtnNext2.Enabled = false;
            this.lbtnPre.CssClass = "unprev";
            this.lbtnPre2.CssClass = "unprev";
            this.lbtnNext.CssClass = "unnext";
            this.lbtnNext2.CssClass = "unnext";
        }
        else if (pgds.PageCount <= 2)
        {
            if (pgds.PageCount == 1)
            {
                this.lbtnNext.Enabled = false;
                this.lbtnNext2.Enabled = false;
                this.lbtnNext.CssClass = "unnext";
                this.lbtnNext2.CssClass = "unnext";
            }
            this.lblCurrentPage.Text = Convert.ToString(pgds.CurrentPageIndex + 1);
            this.lblTotal.Text = pgds.PageCount.ToString();
        }
        else
        {
            this.lblCurrentPage.Text = Convert.ToString(pgds.CurrentPageIndex + 1);
            this.lblTotal.Text = pgds.PageCount.ToString();
        }
    }

    protected void lbtnPre_Click(object sender, EventArgs e)
    {
        //上一页
        ViewState["pagen"] = ((int)ViewState["pagen"]) - 1; //改变页码
        MyDs();
    }
    protected void lbtnNext_Click(object sender, EventArgs e)
    {
        //下一页
        ViewState["pagen"] = ((int)(ViewState["pagen"])) + 1;
        MyDs();
    }

    protected void lbtnType_Click(object sender, EventArgs e)
    {
        string type = ((LinkButton)sender).CommandArgument;
        this.txtType.Text = type;
        MyDs();
        this.panelType.Visible = false;
    }
    protected void lbtnBrand_Click(object sender, EventArgs e)
    {
        string type = this.txtType.Text;
        string brand = ((LinkButton)sender).CommandArgument;
        this.txtBrand.Text = brand;
        MyDs();
        this.panelTable.Visible = false;
    }
}
